"use strict";
const retUtil = require("../utils/commonUtils/retUtil");
const CONSTANT = require("../const/CONST");
const tokenCheckUtil = require('../utils/commonUtils/tokenCheckUtil');

module.exports = function (req, res, next) {
    let token = retUtil.stringFromReq(req,'token');
    if (!token || token.length <=0 ) {
        return retUtil.ret_err_json(CONSTANT.CODE.INVALID_PARAMS,res);
    }
    tokenCheckUtil.checkToken(token,function(err,result){
        if (err){
            console.log("routes/middleware/tokenCheck.js tokenCheck.js tokenCheckUtil err" + err);
            return retUtil.ret_err_json(CONSTANT.CODE.PERMISSION_DENIED,res);
        }
        if (!result) {
            return retUtil.ret_err_json(CONSTANT.CODE.PERMISSION_DENIED,res);
        }else{
            next();
        }
    });
};